package com.hontech.bread.ui.parts

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.FrameLayout
import androidx.recyclerview.widget.RecyclerView
import com.google.android.flexbox.FlexDirection
import com.google.android.flexbox.FlexWrap
import com.google.android.flexbox.FlexboxLayoutManager
import com.hontech.bread.R
import com.hontech.bread.device.mdb.Coin
import com.hontech.bread.device.mdb.CoinDeposited
import com.hontech.bread.device.mdb.CoinDispensed
import com.hontech.bread.device.mdb.CoinInfo
import com.hontech.bread.device.mdb.CoinModelInfo
import com.hontech.bread.device.mdb.CoinSetupInfo
import com.hontech.bread.device.mdb.country
import com.hontech.bread.ui.dialog.runProg
import com.hontech.bread.ui.dialog.showInfo
import com.hontech.bread.ui.fragment.MdbCoinFragment
import com.hontech.bread.ui.view.LabelState
import com.hontech.bread.ui.view.LabelTextView
import com.hontech.bread.utils.EmptyEdge
import com.hontech.bread.utils.hex16
import com.hontech.bread.utils.log
import com.hontech.bread.utils.onClick
import com.hontech.bread.utils.registerEvent
import com.hontech.bread.utils.runOnUi
import com.hontech.bread.utils.stringById
import com.hontech.bread.utils.unregister
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class CoinInfoParts(context: Context, attrs: AttributeSet) : FrameLayout(context, attrs) {

    private val ltLevel: LabelTextView
    private val ltCountryCode: LabelTextView
    private val ltScaling: LabelTextView
    private val ltDecimalPlace: LabelTextView
    private val ltCoinTypeRoute: LabelTextView
    private val rvCoinValue: RecyclerView
    private val btnInit: Button

    private val ltMc: LabelTextView
    private val ltSerial: LabelTextView
    private val ltModel: LabelTextView
    private val ltVersion: LabelTextView
    private val btnUpdate: Button

    init {
        val view = LayoutInflater.from(context).inflate(R.layout.parts_coin_info, this)
        ltLevel = view.findViewById(R.id.id_coin_info_lt_level)
        ltCountryCode = view.findViewById(R.id.id_coin_info_lt_country_code)
        ltScaling = view.findViewById(R.id.id_coin_info_lt_scaling)
        ltDecimalPlace = view.findViewById(R.id.id_coin_info_lt_decimal_places)
        ltCoinTypeRoute = view.findViewById(R.id.id_coin_info_coin_type_route)
        rvCoinValue = view.findViewById(R.id.id_coin_info_rv_coin_value)
        btnInit = view.findViewById(R.id.id_coin_info_btn_init)

        ltMc = view.findViewById(R.id.id_coin_info_lt_mc)
        ltSerial = view.findViewById(R.id.id_coin_info_lt_serial)
        ltModel = view.findViewById(R.id.id_coin_info_lt_model)
        ltVersion = view.findViewById(R.id.id_coin_info_lt_version)
        btnUpdate = view.findViewById(R.id.id_coin_info_btn_update)

        btnInit.onClick { onClickInit() }
        btnUpdate.onClick { onClickUpdate() }

        rvCoinValue.layoutManager = FlexboxLayoutManager(context, FlexDirection.ROW, FlexWrap.WRAP)
        rvCoinValue.edgeEffectFactory = EmptyEdge()
    }

    private suspend fun onClickInit() {
        runProg(stringById(R.string.text_19), stringById(R.string.text_24)) {
            Coin.reset()
            val info = CoinSetupInfo.get()
            ltLevel.set(info.level)
            ltCountryCode.set(country(info.currencyCode))
            ltScaling.set(info.scaling)
            ltDecimalPlace.set(info.decimalPlaces)
            ltCoinTypeRoute.set(info.coinTypeRouting.hex16())
            rvCoinValue.adapter = CoinAdapter(Coin.queryCoinInfo())
            if (info.level != 3) {
                return@runProg
            }
            val modelInfo = CoinModelInfo.get()
            ltMc.set(modelInfo.manufacturerCode)
            ltModel.set(modelInfo.model)
            ltSerial.set(modelInfo.serialNumber)
            ltVersion.set(modelInfo.softVersion)
        }
    }

    private suspend fun onClickUpdate() {
        runProg(stringById(R.string.text_19), stringById(R.string.text_25)) {
            rvCoinValue.adapter = CoinAdapter(Coin.queryCoinInfo())
        }
    }

    override fun onWindowVisibilityChanged(visibility: Int) {
        super.onWindowVisibilityChanged(visibility)
        if (visibility == VISIBLE) {
            registerEvent(this)
        } else {
            unregister(this)
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN_ORDERED)
    fun onCoinDeposited(event: CoinDeposited) {
        if (!MdbCoinFragment.mdbPageFlag) {
            log("coin ${stringById(R.string.t1733460848_398)}")
            return
        }
        runOnUi { showInfo("${stringById(R.string.t1733460848_399)}", event.toString()) }
    }

    @Subscribe(threadMode = ThreadMode.MAIN_ORDERED)
    fun onCoinDispensed(event: CoinDispensed) {
        if (!MdbCoinFragment.mdbPageFlag) {
            log("coin ${stringById(R.string.t1733460848_400)}")
            return
        }
        runOnUi { showInfo("${stringById(R.string.t1733460848_401)}", event.toString()) }
    }

}


private class CoinItem(view: View) : RecyclerView.ViewHolder(view) {

    private val ltCredit = view.findViewById<LabelTextView>(R.id.id_item_coin_credit)
    private val ltNum = view.findViewById<LabelTextView>(R.id.id_item_coin_num)
    private val ltFull = view.findViewById<LabelState>(R.id.id_item_coin_full)
    private val btnEnable = view.findViewById<Button>(R.id.id_item_coin_enable)
    private val btnDisable = view.findViewById<Button>(R.id.id_item_coin_disable)
    private val btnDispense = view.findViewById<Button>(R.id.id_item_coin_dispense)

    fun setUp(info: CoinInfo) {

        ltCredit.set(info.value)
        ltNum.set(info.number)
        ltFull.set(info.full)

        btnEnable.onClick {
            runProg(stringById(R.string.text_20), stringById(R.string.text_20)) {
            }
        }

        btnDisable.onClick {
            runProg(stringById(R.string.text_21), stringById(R.string.text_21)) {
            }
        }

        btnDispense.onClick {
            runProg(stringById(R.string.text_22), stringById(R.string.text_22)) {
                Coin.dispense(info.typeBit, 1)
            }
        }
    }
}


private class CoinAdapter(val infos: List<CoinInfo>) : RecyclerView.Adapter<CoinItem>() {

    override fun onBindViewHolder(holder: CoinItem, position: Int) {
        holder.setUp(infos[position])
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CoinItem {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_coin, parent, false)
        return CoinItem(view)
    }

    override fun getItemCount(): Int {
        return infos.size
    }
}



















